﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace System
{

    public class ZIO
    {

        public static string UploadImage(FileUpload fulHtmlName, string path)
        {

            string filehj = fulHtmlName.FileName.Substring(fulHtmlName.FileName.LastIndexOf('.')).ToLower();
            if (filehj == ".jpg" || filehj == ".jpeg" || filehj == ".gif" || filehj == "swf" || filehj=="")
            {
                Random rd = new Random();
                string HtmlName = DateTime.Now.ToString("yyyyMMddHHmmssfff")+rd.Next(9) + filehj;
                string filepaht = CConfig.ReadAppSettings("UpLoadPath")+path + HtmlName;
                try
                {
                    fulHtmlName.SaveAs(filepaht);
                }
                catch (Exception ex)
                {
                    return "error";
                }
                return HtmlName;
            }
            else
            {
                return "error";
            }

        }

        public static void Save(string path, string info)
        {
            string dir = Path.GetDirectoryName(path);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }

            try
            {
                FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs, System.Text.UnicodeEncoding.UTF8);
                sw.Write(info);
                sw.Close();
                fs.Close();
            }
            catch
            {

            }
        }

        public static void Save(string path, string txt, string encode, FileMode mode)
        {
            string dir = Path.GetDirectoryName(path);
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
            if (!File.Exists(path))
            {
                File.Create(path).Close();
            }
            try
            {
                FileStream fs = new FileStream(path, mode, FileAccess.ReadWrite);
                StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.GetEncoding(encode));
                sw.Write(txt);
                sw.Close();
                fs.Close();
            }
            catch
            {
            }
        }

        public static Dictionary<string, string> ReadToDictionary(string path)
        {
            Dictionary<string, string> result = new Dictionary<string, string>();

            if (File.Exists(path))
            {
                string line = "";
                using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        result[line] = line;
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="Path"></param>
        /// <returns>结果</returns>
        public static string Read(string Path)
        {
            if (!File.Exists(Path))
                return "";
            System.IO.FileStream fs = System.IO.File.OpenRead(Path);
            byte[] file = new Byte[fs.Length];
            fs.Read(file, 0, Convert.ToInt32(fs.Length));
            string s = System.Text.Encoding.Default.GetString(file, 0, file.Length);
            fs.Close();
            return s.Remove(0, 1);
        }
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="Path"></param>
        /// <returns>结果</returns>
        public string _Read(string Path)
        {
            string text = "";
            if (File.Exists(Path))
            {
                using (FileStream fs = File.OpenRead(Path))
                {
                    byte[] file = new Byte[fs.Length];
                    fs.Read(file, 0, file.Length);
                    text = Encoding.UTF8.GetString(file, 0, file.Length);
                }
            }
            return text;
        }

        public static List<string> ReadToList(string path)
        {
            List<string> result = new List<string>();

            if (File.Exists(path))
            {
                string line = "";
                using (StreamReader sr = new StreamReader(path, Encoding.UTF8))
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        result.Add(line);
                    }
                }
            }

            return result;
        }

    }
}
